package cn.com.blackboard.DataModule4B2.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cm.com.blackboard.DataModule4B2.customizeConfigUtils.BatchImportUtil;
import cn.com.blackboard.DataModule4B2.BBDataUtils.UserUtil;
import cn.com.blackboard.DataModule4B2.dao.TeacherInfoMapper;
import cn.com.blackboard.DataModule4B2.model.TeacherInfo;
import cn.com.blackboard.DataModule4B2.service.TeacherService;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel.Status;
import cn.com.blackboard.DataModule4B2.util.Condition;

@Service("TeacherService")
public class TeacherServiceImpl implements TeacherService{

	private static final Logger logger = LoggerFactory.getLogger(TeacherServiceImpl.class);
	
	@Autowired
	private TeacherInfoMapper teacherInfoMapper;

	@Override
	public List<TeacherInfo> getUsers() {
		// TODO Auto-generated method stub
		return teacherInfoMapper.selectByCondition(Condition.SearchAll);
	}

	@Override
	public List<TeacherInfo> getUsers(List<Condition> list) {	
		String conditionStr = Condition.RenderConditoinList(list);
		logger.info(conditionStr);
		return teacherInfoMapper.selectByCondition(conditionStr);
	}

	@Override
	public int insert(TeacherInfo userInfo) {
		// TODO Auto-generated method stub
		return teacherInfoMapper.insert(userInfo);
	}

	@Override
	public int updateStatus(String userId) {
		// TODO Auto-generated method stub
		return teacherInfoMapper.updateRecordImportedByUserId(userId);
	}
	
	@Override
	public List<BatchResultModel> importTeacher(List<TeacherInfo> teacherInfoLst){
		logger.info("start to import teacher");
		List<BatchResultModel> list = new ArrayList<BatchResultModel>();
		int count = 0;
		for(TeacherInfo ti:teacherInfoLst){
			BatchResultModel batchResultModel = new BatchResultModel();
			batchResultModel.setModelClsName(TeacherInfo.class.getName());
			batchResultModel.setModelClsAttr("userId-firstName");
			batchResultModel.setValue(ti.getUserId()+"-"+ti.getFirstName());
			if(ti.getIsimported().equals("N")){
				Status status = UserUtil.importTeacher(ti);
				batchResultModel.setStatus(status);
				count += status.isSuccess()?1:0;
				if(status.isSuccess())
					updateStatus(ti.getUserId());
			}
			else{			
				Status status = UserUtil.updateTeacher(ti);
				batchResultModel.setStatus(status);
				count += status.isSuccess()?1:0;
			}
			list.add(batchResultModel);
		}
		logger.info("teacher need to be imported is "+teacherInfoLst.size()+" the num imported indeed is "+count);
    	return list;
	}

	@Override
	public List<BatchResultModel> batchImportTeacher() {
		String conditionStr = BatchImportUtil.getBatchFilterCondition(TeacherInfo.class.getName());
		List<TeacherInfo> teacherInfoLst = teacherInfoMapper.selectByCondition(conditionStr);
		List<BatchResultModel> list = importTeacher(teacherInfoLst);
		return list;
	}
}
